package com.guiji.job.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guiji.common.core.page.PagePlus;
import com.guiji.job.bo.JobQueryBo;
import com.guiji.job.domain.Job;
import com.guiji.common.core.mybatisplus.core.BaseMapperPlus;
import com.guiji.common.core.mybatisplus.cache.MybatisPlusRedisCache;
import com.guiji.job.vo.JobVo;
import com.guiji.job.vo.UnTagListVo;
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.sql.Wrapper;
import java.util.List;
import java.util.Map;

/**
 * 任务主Mapper接口
 *
 * @author 钟维澎（四月）
 * @date 2021-07-02
 */
// 如使需切换数据源 请勿使用缓存 会造成数据不一致现象
//@CacheNamespace(implementation = MybatisPlusRedisCache.class, eviction = MybatisPlusRedisCache.class)
public interface JobMapper extends BaseMapperPlus<Job> {
	@Select({"<script>",
		"SELECT aa.*,cc.dict_name template_name FROM ",
		"(SELECT c.*,f.file_name file_name FROM cms_job c LEFT join cms_file_resource f on c.file_id=f.file_id WHERE c.del_flag=0 <if test='bo.person != null'>AND c.person = #{bo.person}</if> AND c.status = #{bo.status}",
		"<if test='bo.resourceId != null'>AND c.file_id= #{bo.resourceId}</if>",
		"<if test='bo.templateId != null'>AND c.template_id= #{bo.templateId}</if>",
		"<if test='bo.id != null'>AND c.job_id= #{bo.id}</if>",
		"<if test='bo.fileName != \"\"'>AND f.file_name like concat(concat(\"%\",#{bo.fileName}),\"%\")</if>",
		") aa",
		"LEFT JOIN sys_dict_type cc ON aa.template_id=cc.dict_id",
		"<if test='bo.templateName != \"\"'>WHERE cc.dict_name = #{bo.templateName}</if>",
		"</script>"})
	IPage<UnTagListVo> getUnTagListVo(PagePlus page, @Param("bo") JobQueryBo bo);

	@Select("SELECT c.backup_1,f.upload_path FROM cms_job c JOIN cms_file_resource f on c.file_id=f.file_id WHERE c.status=4 AND c.template_id=#{bo.templateId}")
	List<Map<String, String>> selectEsJson(@Param("bo") JobQueryBo bo);

	@Select("select aya.template_id, aya.`status`,aya.num,dt.dict_name from\n" +
		"(select j.template_id, j.`status`,count(*) as num from cms_job j GROUP BY j.template_id, j.`status` ) aya\n" +
		"LEFT JOIN sys_dict_type dt on aya.template_id = dt.dict_id")
	IPage<Map> countStatus(PagePlus page);

	IPage<JobVo> selectJobPageVo(IPage<?> page,@Param("bo") JobQueryBo bo);
}
